package com.itcanteen.medical.patient.controller;

import com.itcanteen.medical.common.constant.Constant;
import com.itcanteen.medical.common.exception.AdException;
import com.itcanteen.medical.common.vo.CommonResponse;
import com.itcanteen.medical.patient.entity.Medication;
import com.itcanteen.medical.patient.entity.Patient;
import com.itcanteen.medical.patient.service.MedicationService;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;


/**
 * 药品 控制层
 * @author 1804A-TeamOne
 * @date 2020-12-17 16:08
 */
@RestController
@RequestMapping("/medication")
@CrossOrigin
public class MedicationController {

    @Resource
    private MedicationService medicationService;

    @Resource
    private HttpServletRequest httpServletRequest;

    /**
     * 前台传入 是否开启用药提醒  flag==1 开启  flag==0 关闭
     * @author 1804A-TeamOne
     * @date 2020-12-17 14:50
     * @param flag 标记
     * @return com.itcanteen.medical.common.vo.CommonResponse
     */
    @PostMapping("/onOffRemind")
    public CommonResponse<Object> onOffRemind(Integer flag) throws AdException {
        Patient patient = (Patient) httpServletRequest.getSession().getAttribute(PatientController.CURRENT_PATIENT);
        if (patient==null){
            throw new AdException(Constant.ErrorMsg.SESSION_EXPIRATION_ERROR);
        }
        medicationService.onOffRemind(new Medication(patient),flag);
        return new CommonResponse<>(0,flag==1?Constant.PatientConstant.OPEN_NOTIFICATION_SUCCESS:Constant.PatientConstant.CLOSE_NOTIFICATION_SUCCESS);
    }

    /**
     * 查询当前登录人是否已开始用药开启,返回1代表已开启,0代表未开启
     * @author 1804A-TeamOne
     * @date 2020-12-17 14:51
     * @return com.itcanteen.medical.common.vo.CommonResponse
     * @throws AdException 统一异常
     */
    @GetMapping("/findOne")
    public CommonResponse<Object> findOne() throws AdException {
        Patient patient = (Patient) httpServletRequest.getSession().getAttribute(PatientController.CURRENT_PATIENT);
        if (patient==null){
            throw new AdException(Constant.ErrorMsg.SESSION_EXPIRATION_ERROR);
        }
        Medication medication = medicationService.findByPatientId(patient.getId());
        CommonResponse<Object> response = new CommonResponse<>(0, "success",1);
        if(medication==null){
            response.setData(0);
        }
        return response;
    }

}
